Module: Callbacks | .\src\TW\Callbacks.py | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Callback Objects Using Weak ReferencesA common problem in implementing the "Observer" pattern in Python is that containers often want to be an observer of their contents. This makes it tricky to pass callback information to the observed object without creating a circular reference. In limited applications of the observer pattern with relatively static observer-subject relationships, one can code around this, but it would be much nicer if you could just pass a callback object to anything that needed "subscribing" to, and not have to worry about the created references. The The supplied implementations address a variety of low-level issues such
as handling callbacks to objects which do not support weak references
(in which case a normal reference is used). There is also an experimental
Quick usage overview: from TW.Callbacks import Callback, CallbackList class someSubject: def __init__(self): self.observers = CallbackList() self.subscribe = self.observers.subscribe self.unsubscribe = self.observers.remove class anObserver: def __init__(self, aSubject): aSubject.subscribe( Callback(self,'myCallbackMethod') ) See the classes and source code for more details.
|